package org.jumiz.db;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.jumiz.db.config.MySQLConfig;
import org.jumiz.db.http.handler.JumiZOnLineHtmlHandler;

import com.sun.net.httpserver.HttpServer;
import org.jumiz.db.util.DocDBOnLineUtilZ;

public final class JumiZHttpServer {
	
	private static HttpServer server;

	private int port = 22550;
	/**
	 * 够造函数，默认扫描bean下的实体类
	 */
	public JumiZHttpServer() {
		this(null);
	}

	public JumiZHttpServer(MySQLConfig config) {
		DocDBOnLineUtilZ.config = config;
		DocDBOnLineUtilZ.init();
	}
	/**
	 * 创建HTTP服务，默认端口为22550，使用浏览器访问 http://127.0.0.1/22550/jumiz，即可读取到文档内容
	 * @throws IOException
	 */
	public void createServer() throws IOException {
		this.createServer(port);
	}
	/**
	 * 创建HTTP服务，默认端口为22550，使用浏览器访问 http://127.0.0.1/port/jumiz，即可读取到文档内容
	 * @param port	服务端口号
	 * @throws IOException
	 */
	public synchronized void createServer(int port) throws IOException {
		if(server == null) {
			server = HttpServer.create(new InetSocketAddress(port), 0);
			server.createContext("/online", new JumiZOnLineHtmlHandler());
			server.start();
		}
	}
	
	public static void main(String[] args) {
		try {
			MySQLConfig config = new MySQLConfig();
			config.setDriverClass("com.mysql.cj.jdbc.Driver");
			config.setUrl("jdbc:mysql://172.16.55.45:3306/build_bussiness_boot_project?useUnicode=true&characterEncoding=utf-8&user=ccyj&password=123456");

			System.out.println("welcome to use jumiz database document service.");
			new JumiZHttpServer(config).createServer();
			System.out.println("start server success on port " + 22550 + " ...");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
